package com.polaris.live.adapter.backpack

import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import com.bumptech.glide.Glide
import com.polaris.live.R
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.common.ext.autoDp
import com.polaris.live.databinding.ItemRoomBackgroundCommodityBinding
import com.polaris.live.resp.back_resp.PayerShopResp
import com.polaris.live.utils.NinePatchDrawableUtil
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.image.transformRounded

/**
 * 房间背景的商品Adapter
 *
 * @author Created by 天晴 on 2024/7/17/015 17:13
 */
class RoomBackgroundCommodityAdapter : BaseBindingAdapter<ItemRoomBackgroundCommodityBinding, PayerShopResp>() {

    private var mSelectIndex = -1

    override fun convert(
        holder: VBViewHolder<ItemRoomBackgroundCommodityBinding>,
        item: PayerShopResp,
    ) {
        holder.vb.apply {
            val position = holder.absoluteAdapterPosition
            if (position == mSelectIndex) {
                consTitle.setBackgroundResource(R.drawable.back_pack_select_back)
            } else {
                consTitle.setBackgroundResource(R.drawable.r8_back_ffffff)
            }
            Glide.with(icImage.context)
                .load(ImageUtils.imgPath(item.displayUrl))
                .transformRounded(4.autoDp)
                .into(icImage)
            tvName.text = item.shopName

            if (item.cornerMarkUrl == null) {
                discountTv.visibility = View.GONE
            } else {
                discountTv.visibility = View.VISIBLE
                discountTv.text = item.cornerMarkText
                NinePatchDrawableUtil.getNinePatchDrawable(item.cornerMarkUrl,
                    Rect(7.autoDp, 0.autoDp, 7.autoDp, 0.autoDp), { nine ->
                        discountTv.background = nine
                    }) {

                }
            }

            item.coins?.let {
                if (it.isNotEmpty()) {
                    val firstData = it.entries.firstOrNull()
                    firstData?.value?.let { con ->
                        diamondLl.visibility = View.VISIBLE
                        val discountCon = item.discountCoins?.get(firstData.key)
                        if (discountCon != null) {
                            diamondTv.text = "$discountCon"
                            diamondYTv.visibility = View.VISIBLE
                            diamondYTv.text = "$con"
                            diamondYTv.paintFlags =
                                diamondYTv.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
                        } else {
                            diamondTv.text = "$con"
                            diamondYTv.visibility = View.GONE
                        }
                    }
                }
            }
            item.gameCoins?.let {
                if (it.isNotEmpty()) {
                    val firstData = it.entries.firstOrNull()
                    firstData?.value?.let { con ->
                        gameCoinLl.visibility = View.VISIBLE
                        val discountCon = item.discountGameCoins?.get(firstData.key)
                        if (discountCon != null) {
                            gameCoinTv.text = "$discountCon"
                            gameCoinCostTv.visibility = View.VISIBLE
                            gameCoinCostTv.text = "$con"
                            gameCoinCostTv.paintFlags = gameCoinCostTv.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
                        } else {
                            gameCoinTv.text = "$con"
                            gameCoinCostTv.visibility = View.GONE
                        }
                    }
                }
            }
        }
    }

    fun setSelect(position: Int) {
        notifyItemChanged(mSelectIndex)

        mSelectIndex = position

        notifyItemChanged(mSelectIndex)
    }

    fun getLastSelect() = mSelectIndex

    companion object {

        const val NOT_HAVE = -1L             //装扮id等于-1是未获取
    }
}

